using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace Rapier.Kodachi.Shared
{
    public class Host
    {
        #region Data Members
        private IController controller;
        private List<INotifier> notifiers = new List<INotifier>();
        private bool isError = false;
        #endregion

        #region Properties
        private SynchronizationContext _Context;
        public SynchronizationContext Context
        {
           get
           {
              return _Context;
           }
           set
           {
              _Context = value;
           }
        }

        public string UserDataPath
        {
            get
            {
                return Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Rapier Software\\Kodachi");
            }
        }

        private DataManager _DataManager = new DataManager();
        public DataManager DataManager
        {
            get { return _DataManager; }
        }
	
        #endregion

        #region Constructor
        public Host(IController controller)
        {
            this.controller = controller;
            controller.Host = this;
        }
        #endregion

        #region Public Methods
        public void Notify(NotificationPackage package)
        {
           if (notifiers.Count > 0)
            {
                foreach (INotifier notifier in notifiers)
                {
                   Context.Send( new SendOrPostCallback( delegate( object state )
                   {
                      notifier.Notify( package );
                   } ), null );
                }
            }
            else
            {
                MessageBoxIcon icon = MessageBoxIcon.Information;
                if (isError)
                {
                    icon = MessageBoxIcon.Error;
                    isError = false;
                }
                Context.Send( new SendOrPostCallback( delegate( object state )
                {
                   MessageBox.Show( package.Message, package.Caption, MessageBoxButtons.OK, icon );
                } ), null );
             }
        }

        public void NotifyError(string errorMessage)
        {
            NotifyError("Error", errorMessage);
        }

        public void NotifyError(string caption, string errorMessage)
        {
            isError = true;
            NotificationPackage pkg = new NotificationPackage();
            pkg.Image = System.Drawing.SystemIcons.Error.ToBitmap();
            pkg.Caption = caption;
            pkg.Message = errorMessage;
            Notify(pkg);
        }

        public void AddItem(Item item)
        {
            controller.AddItem(item);
        }

        public void RemoveItem(Item item)
        {
            controller.RemoveItem(item);
        }

        public bool ItemExists(Item item)
        {
            return controller.ItemExists(item);
        }

        public void AddAction(Action action)
        {
            controller.AddAction(action);
        }

        public void RemoveAction(Action action)
        {
            controller.RemoveAction(action);
        }

        public bool ActionExists(Action action)
        {
            return controller.ActionExists(action);
        }

        public void AddNotifier(INotifier notifier)
        {
            if(!notifiers.Contains(notifier))
                notifiers.Add(notifier);
        }

        public void RemoveNotifier(INotifier notifier)
        {
            if(notifiers.Contains(notifier))
                notifiers.Remove(notifier);
        }

        public bool NotifierExists(INotifier notifier)
        {
            return notifiers.Contains(notifier);
        }
        #endregion

    }
}
